package org.gten.gegedexin.controller;

import org.gten.gegedexin.Data.Mail;
import org.gten.gegedexin.service.ReceiveFile;
import org.gten.gegedexin.service.SendEmail;
import org.gten.gegedexin.service.VerifyMail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class SendEmailController {

    @Autowired
    VerifyMail verifymail;
    @Autowired
    SendEmail sendEmail;
    @Autowired
    ReceiveFile receivefile;

    //邮件id，用于识别紧跟在邮件后的附件
    int id;

    //  从前端接收邮件，判断邮件格式是否正确，若正确则将其发出
    @PostMapping("/sendMail")
    @CrossOrigin
    public int sendMail(@RequestBody Mail mail) throws Exception {
        // System.out.println(123);
        System.out.println("sendMail");
        System.out.println(mail);
        // 判断收件人是否存在
        if(verifymail.verifyReceiver(mail.getReceiver())){
            //  将邮件发出，同时记录该邮件的id
            id = sendEmail.sendmail(mail);
            return 1;
        }
        else return 0;
    }

    //  接收邮件的附件
    @PostMapping("/sendAppendix")
    @CrossOrigin
    public void sendAppendix(MultipartFile file) throws Exception {
        receivefile.addfile(id,file);
    }
}




